package com.mall.manager.sellstatic.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mall.common.attributes.CommonAttributes;
import com.mall.common.entity.SellStaticstic;
import com.mall.manager.sellstatic.service.SellStaticService;

/**
 * 全国园所按地区统计
 * @author tgy
 *
 */
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/manager")
@Controller
public class SellStatiController {

	@Resource
	SellStaticService sellStaticService;
	
	/**
	 * 全国园所数量地图显示
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/sellMap")
	public String findSellStatics(HttpServletRequest request,ModelMap modelMap){
		List  list = sellStaticService.findBabyTop(request);
		Map<String,String> mp = new HashMap<String,String>();
		Map<String,String> province = CommonAttributes.getMap();
		if(list!=null&&list.size()>0){
			JSONArray ar = JSONArray.fromObject(list);
			for(int i=0;i<ar.size();i++){
				Map<String,Object> map = JSONObject.fromObject(ar.get(i));
				SellStaticstic se = new SellStaticstic();
				String areaName = MapUtils.getString(map, "area_name");
				se.setId(MapUtils.getInteger(map, "id"));
				se.setAreaId(MapUtils.getString(map, "area_id"));
				se.setAreaName(areaName);
				se.setGardenSum(MapUtils.getInteger(map, "garden_sum"));
				se.setPublicSum(MapUtils.getInteger(map, "public_sum"));
				se.setPrivateSum(MapUtils.getInteger(map, "private_sum"));
				se.setAgentSum(MapUtils.getInteger(map, "agent_sum"));
				se.setOwnSum(MapUtils.getInteger(map, "own_sum"));
				modelMap.addAttribute(areaName, se);
				//保存存在的省
				mp.put(areaName, areaName);
			}
		}
		//存在省份数据为0的情况
		if(mp.size()>0&&mp.size()!=34){
			for(String key:province.keySet()){
				if(!mp.containsKey(key)){
					SellStaticstic se = new SellStaticstic();
					se.setId(-1);
					se.setAreaId("-1");
					se.setAreaName("无");
					se.setGardenSum(0);
					se.setPublicSum(0);
					se.setPrivateSum(0);
					se.setAgentSum(0);
					se.setOwnSum(0);
					modelMap.addAttribute(key, se);
				}
			}
		}
		return "sellstatics/sellstatics_map";
	}
}











